<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>第六章-构建基础 Chapter&nbsp;6.&nbsp;Build Script Basics</title>
<link xmlns:xslthl="http://xslthl.sf.net" type="text/css"
	rel="stylesheet" href="../css/base.css">
<link xmlns:xslthl="http://xslthl.sf.net" type="text/css"
	rel="stylesheet" href="../css/docs.css">
<link xmlns:xslthl="http://xslthl.sf.net" type="text/css"
	rel="stylesheet" href="../css/userguide.css">
<meta content="DocBook XSL Stylesheets V1.75.2" name="generator">
<link rel="home" href="../userguide.html" title="Gradle User Guide">
<link rel="up" href="../userguide.html" title="Gradle User Guide">
<link rel="prev" href="../ch05/troubleshooting.html"
	title="Chapter&nbsp;5.&nbsp;Troubleshooting">
<link rel="next" href="../ch07/tutorial_java_projects.html"
	title="Chapter&nbsp;7.&nbsp;Java Quickstart">
</head>
<body>
	<div class="navheader">
		<div>
			<div class="navbar">
				<a xmlns:xslthl="http://xslthl.sf.net"
					href="../ch05/troubleshooting.html"
					title="Chapter&nbsp;5.&nbsp;Troubleshooting">前一页</a><span>|</span><a
					xmlns:xslthl="http://xslthl.sf.net" href="../userguide.html"
					title="Gradle User Guide">返回目录</a><span>|</span><a
					xmlns:xslthl="http://xslthl.sf.net"
					href="../ch07/tutorial_java_projects.html"
					title="Chapter&nbsp;7.&nbsp;Java Quickstart">后一页</a>
			</div>
		</div>
	</div>
	<div class="chapter">
		<div class="titlepage">
			<div>
				<div>
					<h1 xmlns:xslthl="http://xslthl.sf.net">
						<a name="tutorial_using_tasks"></a> 第六章.构建基础
					</h1>
					<h3 xmlns:xslthl="http://xslthl.sf.net">
						<a name="tutorial_using_tasks"></a> Chapter&nbsp;6.&nbsp;Build
						Script Basics
					</h3>
				</div>
			</div>
		</div>
		<div class="section">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title">
							<a name="N101A5"></a>6.1.&nbsp;Projects 和 tasks
						</h2>
						<h4 class="title">
							<a name="N101A5"></a>6.1.&nbsp;Projects and tasks
						</h4>
					</div>
				</div>
			</div>
			<p>
				<em class="firstterm"> projects</em> 和 <em class="firstterm">tasks</em>是Gradle中最重要的两个概念<br>
				Everything in Gradle sits on top of two basic concepts: <em
					class="firstterm">projects</em> and <em class="firstterm">tasks</em>.
			</p>
			<p>
				任何一个Gradle构建都是由一个或多个 <em class="firstterm">projects</em>
				组成.每个project包括许多可构建组成部分.
				这完全取决于你要构建些什么.举个栗子.每个project或许是一个jar包或者一个web应用,
				它也可以是一个由许多其他项目中产生的jar构成的zip压缩包.一个project不必描述它只能进行构建操作.
				它也可以部署你的应用或搭建你的环境.不要担心它像听上去的那样庞大.
				Gradle的build-by-convention可以让您来具体定义一个project到底该做什么.<br> Every
				Gradle build is made up of one or more <em class="firstterm">projects</em>.
				What a project represents depends on what it is that you are doing
				with Gradle. For example, a project might represent a library JAR or
				a web application. It might represent a distribution ZIP assembled
				from the JARs produced by other projects. A project does not
				necessarily represent a thing to be built. It might represent a
				thing to be done, such as deploying your application to staging or
				production environments. Don't worry if this seems a little vague
				for now. Gradle's build-by-convention support adds a more concrete
				definition for what a project is.
			</p>
			<p>
				每个project都由多个tasks组成.每个task都代表了构建执行过程中的一个原子性操作.如编译,打包,生成javadoc,发布到某个仓库等操作<br>
				Each project is made up of one or more <em class="firstterm">tasks</em>.
				A task represents some atomic piece of work which a build performs.
				This might be compiling some classes, creating a JAR, generating
				javadoc, or publishing some archives to a repository.
			</p>
			<p>
				到目前为止,可以发现我们可以在一个project中定义一些简单任务,后续章节将会阐述多项目构建和多项目多任务的内容.<br>
				For now, we will look at defining some simple tasks in a build with
				one project. Later chapters will look at working with multiple
				projects and more about working with projects and tasks.
			</p>
		</div>
		<div class="section">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title">
							<a name="N101BC"></a>6.2.&nbsp;Hello world
						</h2>
						<h4 class="title">
							<a name="N101BC"></a>6.2.&nbsp;Hello world
						</h4>
					</div>
				</div>
			</div>
			<p>
				你可以通过在命令行运行<span class="command"><strong>gradle</strong></span>命令来执行构建,
				<span class="command"><strong>gradle</strong></span>命令会从当前目录下寻找
				<code class="filename">build.gradle</code>
				文件来执行构建.我们称
				<code class="filename">build.gradle</code>
				文件为<em class="firstterm">构建脚本</em>.
				严格来说这其实是一个构建配置脚本,后面你会了解到这个构建脚本定义了一个project和一些默认的task<br> You
				run a Gradle build using the <span class="command"><strong>gradle</strong></span>
				command. The <span class="command"><strong>gradle</strong></span>
				command looks for a file called
				<code class="filename">build.gradle</code>
				in the current directory. <sup>[<a href="#ftn.N101CA"
					name="N101CA" class="footnote">2</a>]
				</sup> We call this
				<code class="filename">build.gradle</code>
				file a <em class="firstterm">build script</em>, although strictly
				speaking it is a build configuration script, as we will see later.
				The build script defines a project and its tasks.
			</p>
			<p>
				你可以创建如下脚本到
				<code class="filename">build.gradle</code>
				中 To try this out, create the following build script named
				<code class="filename">build.gradle</code>
				.
			</p>
			<div class="example">
				<a name="hello"></a>
				<p class="title">
					<b> 例6.1. 第一个构建脚本<br> Example&nbsp;6.1.&nbsp;The first
						build script
					</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">task hello {
    doLast {
        println <span xmlns:xslthl="http://xslthl.sf.net"
							class="hl-string">'Hello world!'</span>
    }
}</pre>
				</div>
			</div>
			<br class="example-break">
			<p>
				然后在该文件所在目录执行 gradle -q hello<br> In a command-line shell, enter
				into the containing directory and execute the build script by
				running <strong class="userinput"><code>gradle -q
						hello</code></strong>:
			</p>
			<div class="tip">
				<h3 class="title">
					<code class="option">-q</code>
					参数的作用是什么?<br>
				</h3>
				<h5 class="title">
					What does
					<code class="option">-q</code>
					do?
				</h5>
				<p>
					该文档的示例中很多地方在调用gradle命令时都加了
					<code class="option">-q</code>
					参数. 该参数用来控制gradle的日志级别,可以保证只输出我们需要的内容. 具体可参阅本文档 <a class="xref"
						href="../logging.html"> 第十八章 <i>日志 </i></a>来了解更多参数和信息.<br>
					Most of the examples in this user guide are run with the
					<code class="option">-q</code>
					command-line option. This suppresses Gradle's log messages, so that
					only the output of the tasks is shown. This keeps the example
					output in this user guide a little clearer. You don't need to use
					this option if you don't want. See <a class="xref"
						href="../logging.html">Chapter&nbsp;18, <i>Logging</i></a> for
					more details about the command-line options which affect Gradle's
					output.
				</p>
			</div>
			<div class="example">
				<a name="hello"></a>
				<p class="title">

					<b> 例6.2. 执行脚本<br> Example&nbsp;6.2.&nbsp;Execution of a
						build script
					</b>
				</p>
				<div class="example-contents">
					<p>
						Output of <strong class="userinput"><code>gradle
								-q hello</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q hello
Hello world!</pre>
				</div>
			</div>
			<br class="example-break">
			<p>
				上面的脚本定义了一个叫做
				<code class="literal">hello</code>
				的 task,并且给它添加了一个动作. 当执行<strong class="userinput"><code>gradle
						hello</code></strong>的时候, Gralde便会去调用
				<code class="literal">hello</code>
				这个任务来执行给定操作.这些操作其实就是一个用groovy书写的闭包<br> What's going on here?
				This build script defines a single task, called
				<code class="literal">hello</code>
				, and adds an action to it. When you run <strong class="userinput"><code>gradle
						hello</code></strong>, Gradle executes the
				<code class="literal">hello</code>
				task, which in turn executes the action you've provided. The action
				is simply a closure containing some Groovy code to execute.
			</p>
			<p>
				如果你觉得它看上去跟Ant中的targets很像,那么是对的.
				Gradle的tasks就相当于Ant中的targets.不过你会发现他功能更加强大.
				我们只是换了一个比target更形象的另外一个术语.不幸的是这恰巧与Ant中的术语有些冲突. ant
				命令中有诸如javac、copy、tasks.所以当该文档中提及tasks时,除非特别指明 <span class="emphasis"><em>ant
						task</em></span>. 否则指的<span class="emphasis"><em>均是</em>指Gradle中的tasks.<br>
					If you think this looks similar to Ant's targets, well, you are
					right. Gradle tasks are the equivalent to Ant targets. But as you
					will see, they are much more powerful. We have used a different
					terminology than Ant as we think the word <span class="emphasis"><em>task</em></span>
					is more expressive than the word <span class="emphasis"><em>target</em></span>.
					Unfortunately this introduces a terminology clash with Ant, as Ant
					calls its commands, such as <code class="literal">javac</code> or <code
						class="literal">copy</code> , tasks. So when we talk about tasks,
					we <span class="emphasis"><em>always</em></span> mean Gradle tasks,
					which are the equivalent to Ant's targets. If we talk about Ant
					tasks (Ant commands), we explicitly say <span class="emphasis"><em>ant
							task</em></span>. 
			</p>
		</div>
		<div class="section">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title">
							<a name="N10222"></a>6.3.&nbsp;快速定义任务
						</h2>
						<h4 class="title">
							<a name="N10222"></a>6.3.&nbsp;A shortcut task definition
						</h4>

					</div>
				</div>
			</div>
			<p>
				用一种更简洁的方式来定义 上面的
				<code class="literal">hello</code>
				任务.<br> There is a shorthand way to define a task like our
				<code class="literal">hello</code>
				task above, which is more concise.
			</p>
			<div class="example">
				<a name="helloShortcut"></a>
				<p class="title">
					<b> 例6.3.快速定义任务<br> Example&nbsp;6.3.&nbsp;A task
						definition shortcut
					</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">task hello &lt;&lt; {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'Hello world!'</span>
}</pre>
				</div>
			</div>
			<br class="example-break">
			<p>
				上面的脚本又一次采用闭包的方式来定义了一个叫做hello的任务,本文档后续章节中我们将会更多的采用这种风格来定义任务.<br>
				Again, this defines a task called
				<code class="literal">hello</code>
				with a single closure to execute. We will use this task definition
				style throughout the user guide.
			</p>
		</div>
		<div class="section">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title">
							<a name="N10239"></a>6.4.&nbsp;代码即脚本
						</h2>
						<h4 class="title">
							<a name="N10239"></a>6.4.&nbsp;Build scripts are code
						</h4>
					</div>
				</div>
			</div>
			<p>Gradle脚本采用Groovy书写,作为开胃菜,看下下面这个栗子. Gradle's build scripts
				expose to you the full power of Groovy. As an appetizer, have a look
				at this:</p>
			<div class="example">
				<a name="upper"></a>
				<p class="title">
					<b> •例6.4 在gradle任务中采用groovy<br>
						Example&nbsp;6.4.&nbsp;Using Groovy in Gradle's tasks
					</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">task upper &lt;&lt; {
    String someString = <span xmlns:xslthl="http://xslthl.sf.net"
							class="hl-string">'mY_nAmE'</span>
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">"Original: "</span> + someString 
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">"Upper case: "</span> + someString.toUpperCase()
}</pre>
					<p>
						Output of <strong class="userinput"><code>gradle
								-q upper</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q upper
Original: mY_nAmE
Upper case: MY_NAME</pre>
				</div>
			</div>
			<br class="example-break">
			<p>
				或者<br>or
			</p>
			<div class="example">
				<a name="count"></a>
				<p class="title">
					•例6.5 在gradle任务中采用groovy<br> <b>Example&nbsp;6.5.&nbsp;Using
						Groovy in Gradle's tasks</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">task count &lt;&lt; {
    4.times { print <span xmlns:xslthl="http://xslthl.sf.net"
							class="hl-string">"$it "</span> }
}</pre>
					<p>
						Output of <strong class="userinput"><code>gradle
								-q count</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q count
0 1 2 3</pre>
				</div>
			</div>
			<br class="example-break">
		</div>
		<div class="section">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title">
							<a name="sec:task_dependencies"></a>6.5.&nbsp;任务依赖
						</h2>
						<h4 class="title">
							<a name="sec:task_dependencies"></a>6.5.&nbsp;Task dependencies
						</h4>
					</div>
				</div>
			</div>
			<p>
				你可以按如下方式创建任务间的依赖关系<br> As you probably have guessed, you can
				declare dependencies between your tasks.
			</p>
			<div class="example">
				<a name="intro"></a>
				<p class="title">
					<b> 在两个任务之间指明依赖关系<br> Example&nbsp;6.6.&nbsp;Declaration
						of dependencies between tasks
					</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">task hello &lt;&lt; {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'Hello world!'</span>
}
task intro(dependsOn: hello) &lt;&lt; {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">"I'm Gradle"</span>
}</pre>
					<p>
						<strong class="userinput"><code>gradle -q intro</code></strong>的输出结果<br>
						Output of <strong class="userinput"><code>gradle
								-q intro</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q intro
Hello world!
I'm Gradle</pre>
				</div>
			</div>
			<br class="example-break">
			<p>
				添加依赖task也可以不必首先声明被依赖的task.<br> To add a dependency, the
				corresponding task does not need to exist.
			</p>
			<div class="example">
				<a name="lazyDependsOn"></a>
				<p class="title">
					<b>Example&nbsp;6.7.&nbsp;延迟依赖 .Lazy dependsOn - the other task
						does not exist (yet)</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">task taskX(dependsOn: <span
							xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'taskY'</span>) &lt;&lt; {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'taskX'</span>
}
task taskY &lt;&lt; {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'taskY'</span>
}</pre>
					<p>
						Output of <strong class="userinput"><code>gradle
								-q taskX</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q taskX
taskY
taskX</pre>
				</div>
			</div>
			<br class="example-break">
			<p>
				可以看到,
				<code class="literal">taskX</code>
				是 在
				<code class="literal">taskY</code>
				之前定义的,这在多项目构建中非常有用 关于任务依赖的更多信息可以查看<a class="xref"
					href="../more_about_tasks.html#sec:adding_dependencies_to_tasks">章节&nbsp;15.4,
					&ldquo;给任务添加依赖&rdquo;</a>..<br> The dependency of
				<code class="literal">taskX</code>
				to
				<code class="literal">taskY</code>
				is declared before
				<code class="literal">taskY</code>
				is defined. This is very important for multi-project builds. Task
				dependencies are discussed in more detail in <a class="xref"
					href="../more_about_tasks.html#sec:adding_dependencies_to_tasks">Section&nbsp;15.4,
					&ldquo;Adding dependencies to a task&rdquo;</a>.
			</p>
			<p>
				注意:当引用的任务尚未定义的时候不可使用短标记法(see <a class="xref"
					href="../tutorial_using_tasks.html#sec:shortcut_notations">章节&nbsp;6.8,
					&ldquo;短标记法&rdquo;</a>) 来运行任务.<br> Please notice that you can't
				use shortcut notation (see <a class="xref"
					href="../tutorial_using_tasks.html#sec:shortcut_notations">Section&nbsp;6.8,
					&ldquo;Shortcut notations&rdquo;</a>) when referring to a task that is
				not yet defined.
			</p>
		</div>
		<div class="section">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title">
							<a name="N1029B"></a>6.6.&nbsp;动态任务
						</h2>
						<h4 class="title">
							<a name="N1029B"></a>6.6.&nbsp;Dynamic tasks
						</h4>
					</div>
				</div>
			</div>
			<p>
				借助Groovy的强大不仅可以定义简单任务还能做更多的事.例如,可以动态定义任务.<br> The power of
				Groovy can be used for more than defining what a task does. For
				example, you can also use it to dynamically create tasks.
			</p>
			<div class="example">
				<a name="dynamic"></a>
				<p class="title">
					<b> 例 6.8. 创建动态任务<br> Example&nbsp;6.8.&nbsp;Dynamic
						creation of a task
					</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">4.times { counter -&gt;
    task <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">"task$counter"</span> &lt;&lt; {
        println <span xmlns:xslthl="http://xslthl.sf.net"
							class="hl-string">"I'm task number $counter"</span>
    }
}</pre>
					<p>

						<strong class="userinput"><code>gradle -q task1</code></strong>的输出结果.<br>
						Output of <strong class="userinput"><code>gradle
								-q task1</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q task1
I'm task number 1</pre>
				</div>
			</div>
			<br class="example-break">
		</div>
		<div class="section">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title">
							<a name="N102B0"></a>6.7.&nbsp;任务操纵
						</h2>
						<h4 class="title">
							<a name="N102B0"></a>6.7.&nbsp;Manipulating existing tasks
						</h4>
					</div>
				</div>
			</div>
			<p>
				一旦任务被创建后,任务之间可以通过API进行相互访问.这也是与Ant的不同之处.比如可以增加一些依赖.<br> Once
				tasks are created they can be accessed via an <span class="emphasis"><em>API</em></span>.
				This is different to Ant. For example you can create additional
				dependencies.
			</p>
			<div class="example">
				<a name="dynamicDepends"></a>
				<p class="title">
					<b> 例 6.9. 通过API进行任务之间的通信 - 增加依赖<br>
						Example&nbsp;6.9.&nbsp;Accessing a task via API - adding a
						dependency
					</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">4.times { counter -&gt;
    task <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">"task$counter"</span> &lt;&lt; {
        println <span xmlns:xslthl="http://xslthl.sf.net"
							class="hl-string">"I'm task number $counter"</span>
    }
}
task0.dependsOn task2, task3</pre>
					<p>
						<strong class="userinput"><code>gradle -q task0</code></strong>的输出结果.<br>
						Output of <strong class="userinput"><code>gradle
								-q task0</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q task0
I'm task number 2
I'm task number 3
I'm task number 0</pre>
				</div>
			</div>
			<br class="example-break">
			<p>
				为已存在的任务增加行为.<br> Or you can add behavior to an existing task.
			</p>
			<div class="example">
				<a name="helloEnhanced"></a>
				<p class="title">
					<b> 例 6.10. 通过API进行任务之间的通信 - 增加任务行为<br>
						Example&nbsp;6.10.&nbsp;Accessing a task via API - adding
						behaviour
					</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">task hello &lt;&lt; {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'Hello Earth'</span>
}
hello.doFirst {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'Hello Venus'</span>
}
hello.doLast {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'Hello Mars'</span>
}
hello &lt;&lt; {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'Hello Jupiter'</span>
}</pre>
					<p>
						Output of <strong class="userinput"><code>gradle
								-q hello</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q hello
Hello Venus
Hello Earth
Hello Mars
Hello Jupiter</pre>
				</div>
			</div>
			<br class="example-break">
			<p>
				<code class="literal">doFirst</code>
				和
				<code class="literal">doLast</code>
				可以进行多次调用. 他们分别被添加在任务的开头和结尾.当任务开始执行时这些动作会按照既定顺序进行.其中
				<code class="literal">&lt;&lt;</code>
				操作符 是
				<code class="literal">doLast</code>
				的简写方式.<br> The calls
				<code class="literal">doFirst</code>
				and
				<code class="literal">doLast</code>
				can be executed multiple times. They add an action to the beginning
				or the end of the task's actions list. When the task executes, the
				actions in the action list are executed in order. The
				<code class="literal">&lt;&lt;</code>
				operator is simply an alias for
				<code class="literal">doLast</code>
				.
			</p>
		</div>
		<div class="section">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title">
							<a name="sec:shortcut_notations"></a>6.8.&nbsp;短标记法
						</h2>
						<h4 class="title">
							<a name="sec:shortcut_notations"></a>6.8.&nbsp;Shortcut notations
						</h4>
					</div>
				</div>
			</div>
			<p>
				你早就注意到了吧，没错，每个任务都是一个脚本的属性，你可以访问它:<br> As you might have noticed
				in the previous examples, there is a convenient notation for
				accessing an <span class="emphasis"><em>existing</em></span> task.
				Each task is available as a property of the build script:
			</p>
			<div class="example">
				<a name="helloWithShortCut"></a>
				<p class="title">
					<b> 例 6.11. 以属性的方式访问任务<br>
						Example&nbsp;6.11.&nbsp;Accessing task as a property of the build
						script
					</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">task hello &lt;&lt; {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'Hello world!'</span>
}
hello.doLast {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">"Greetings from the $hello.name task."</span>
}</pre>
					<p>
						<strong class="userinput"><code>gradle -q hello</code></strong>的输出结果<br>
						Output of <strong class="userinput"><code>gradle
								-q hello</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q hello
Hello world!
Greetings from the hello task.</pre>
				</div>
			</div>
			<br class="example-break">
			<p>
				对于插件提供的内置任务。这尤其方便(例如:complie)<br> This enables very readable
				code, especially when using the out of the box tasks provided by the
				plugins (e.g.
				<code class="literal">compile</code>
				).
			</p>
		</div>
		<div class="section">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title">
							<a name="sec:extra_task_properties"></a>6.9.&nbsp;增加自定义属性
						</h2>
						<h4 class="title">
							<a name="sec:extra_task_properties"></a>6.9.&nbsp;Extra task properties
						</h4>
					</div>
				</div>
			</div>
			<p>
				你可以为一个任务添加额外的属性.例如,新增一个叫做
				<code class="literal">myProperty</code>
				的属性,用
				<code class="literal">ext.myProperty</code>
				的方式给他一个初始值.这样便增加了一个自定义属性.<br> You can add your own properties
				to a task. To add a property named
				<code class="literal">myProperty</code>
				, set
				<code class="literal">ext.myProperty</code>
				to an initial value. From that point on, the property can be read
				and set like a predefined task property.
			</p>
			<div class="example">
				<a name="extraTaskProperties"></a>
				<p class="title">
					<b> 例 6.12. 为任务增加自定义属性<br> Example&nbsp;6.12.&nbsp;Adding
						extra properties to a task
					</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">task myTask {
    ext.myProperty = <span xmlns:xslthl="http://xslthl.sf.net"
							class="hl-string">"myValue"</span>
}

task printTaskProperties &lt;&lt; {
    println myTask.myProperty
}</pre>
					<p>
						<strong class="userinput"><code>gradle -q
								printTaskProperties</code></strong>的输出结果<br> Output of <strong
							class="userinput"><code>gradle -q
								printTaskProperties</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q printTaskProperties
myValue</pre>
				</div>
			</div>
			<br class="example-break"> 自定义属性不仅仅局限于任务上,还可以做更多事情.你可以阅读<a
				class="xref"
				href="../writing_build_scripts.html#sec:extra_properties">章节&nbsp;13.4.2,
				&ldquo;自定义属性&rdquo;</a>来了解更多内容.<br> Extra properties aren't limited
			to tasks. You can read more about them in <a class="xref"
				href="../writing_build_scripts.html#sec:extra_properties">Section&nbsp;13.4.2,
				&ldquo;Extra properties&rdquo;</a>.
		</div>
		<div class="section">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title">
							<a name="N10326"></a>6.10.&nbsp;调用Ant任务
						</h2>
						<h4 class="title">
                            <a name="N10326"></a>6.10.&nbsp;Using Ant Tasks
                        </h4>
					</div>
				</div>
			</div>
			<p>
			    Ant任务是Gradle中的一等公民.Gradle借助Groovy对Ant任务进行了优秀的整合.
			    Gradle自带了一个<code class="literal">AntBuilder</code>,在Gradle中调用Ant任务比在
			    <code class="filename">build.xml</code>中调用更加的方便和强大.
			         通过下面的例子你可以学到如何调用一个Ant任务以及如何与Ant中的属性进行通信.<br>
				Ant tasks are first-class citizens in Gradle. Gradle provides
				excellent integration for Ant tasks by simply relying on Groovy.
				Groovy is shipped with the fantastic
				<code class="literal">AntBuilder</code>
				. Using Ant tasks from Gradle is as convenient and more powerful
				than using Ant tasks from a
				<code class="filename">build.xml</code>
				file. From the example below, you can learn how to execute ant tasks
				and how to access ant properties:
			</p>
			<div class="example">
				<a name="antLoadfile"></a>
				<p class="title">
					<b>
					例 6.13. 利用AntBuilder执行 ant.loadfile
					<br>
					Example&nbsp;6.13.&nbsp;Using AntBuilder to execute
						ant.loadfile target</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">task loadfile &lt;&lt; {
    def files = file(<span xmlns:xslthl="http://xslthl.sf.net"
							class="hl-string">'../antLoadfileResources'</span>).listFiles().sort()
    files.each { File file -&gt;
        <span xmlns:xslthl="http://xslthl.sf.net" class="hl-keyword">if</span> (file.isFile()) {
            ant.loadfile(srcFile: file, property: file.name)
            println <span xmlns:xslthl="http://xslthl.sf.net"
							class="hl-string">" *** $file.name ***"</span>
            println <span xmlns:xslthl="http://xslthl.sf.net"
							class="hl-string">"${ant.properties[file.name]}"</span>
        }
    }
}</pre>
					<p>
					    <strong class="userinput"><code>gradle
                                -q loadfile</code></strong>的输出结果
                        <br>
						Output of <strong class="userinput"><code>gradle
								-q loadfile</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q loadfile
*** agile.manifesto.txt ***
Individuals and interactions over processes and tools
Working software over comprehensive documentation
Customer collaboration  over contract negotiation
Responding to change over following a plan
 *** gradle.manifesto.txt ***
Make the impossible possible, make the possible easy and make the easy elegant.
(inspired by Moshe Feldenkrais)</pre>
				</div>
			</div>
			<br class="example-break">
			<p>
			         在你脚本里还可以利用Ant做更多的事情.想了解更多请参阅 
			         <a class="xref" href="../ant.html">章节&nbsp;17,
                    <i>在Gradle中调用Ant</i></a><br>
				There is lots more you can do with Ant in your build scripts. You
				can find out more in <a class="xref" href="../ant.html">Chapter&nbsp;17,
					<i>Using Ant from Gradle</i>
				</a>.
			</p>
		</div>
		<div class="section">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title">
							<a name="N10346"></a>6.11.&nbsp;方法抽取
						</h2>
						<h4 class="title">
                            <a name="N10346"></a>6.11.&nbsp;Using methods
                        </h4>
					</div>
				</div>
			</div>
			<p>
			Gradle的强大要看你如何编写脚本逻辑.针对上面的例子,首先要做的就是要抽取方法.
			<br>
			Gradle scales in how you can organize your build logic. The
				first level of organizing your build logic for the example above, is
				extracting a method.</p>
			<div class="example">
				<a name="antLoadfileWithMethod"></a>
				<p class="title">
					<b>
					例 6.14 利用方法组织脚本逻辑<br>
					Example&nbsp;6.14.&nbsp;Using methods to organize your build
						logic</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">task checksum &lt;&lt; {
    fileList(<span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'../antLoadfileResources'</span>).each {File file -&gt;
        ant.checksum(file: file, property: <span
							xmlns:xslthl="http://xslthl.sf.net" class="hl-string">"cs_$file.name"</span>)
        println <span xmlns:xslthl="http://xslthl.sf.net"
							class="hl-string">"$file.name Checksum: ${ant.properties["</span>cs_$file.name<span
							xmlns:xslthl="http://xslthl.sf.net" class="hl-string">"]}"</span>
    }
}

task loadfile &lt;&lt; {
    fileList(<span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'../antLoadfileResources'</span>).each {File file -&gt;
        ant.loadfile(srcFile: file, property: file.name)
        println <span xmlns:xslthl="http://xslthl.sf.net"
							class="hl-string">"I'm fond of $file.name"</span>
    }
}

File[] fileList(String dir) {
    file(dir).listFiles({file -&gt; file.isFile() } as FileFilter).sort()
}</pre>
					<p>
					<strong class="userinput"><code>gradle
                                -q loadfile</code></strong>的输出结果<br>
						Output of <strong class="userinput"><code>gradle
								-q loadfile</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q loadfile
I'm fond of agile.manifesto.txt
I'm fond of gradle.manifesto.txt</pre>
				</div>
			</div>
			<br class="example-break">
			<p>
			    在后面的章节你会看到类似出去出来的方法可以在多项目构建中的子项目中调用.
			    无论构建逻辑多复杂,Gradle都可以提供给你一种简便的方式来组织它们.想了解更多请参阅
			     <a class="xref"
                    href="../organizing_build_logic.html">章节&nbsp;59, <i>组织构建逻辑</i></a>.
               <br>
				Later you will see that such methods can be shared among subprojects
				in multi-project builds. If your build logic becomes more complex,
				Gradle offers you other very convenient ways to organize it. We have
				devoted a whole chapter to this. See <a class="xref"
					href="../organizing_build_logic.html">Chapter&nbsp;59, <i>Organizing
						Build Logic</i></a>.
			</p>
		</div>
		<div class="section">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title">
							<a name="sec:default_tasks"></a>6.12.&nbsp;定义默认任务
						</h2>
						<h4 class="title">
                            <a name="sec:default_tasks"></a>6.12.&nbsp;Default tasks
                        </h4>
					</div>
				</div>
			</div>
			<p>
			Gradle允许在脚本中定义多个默认任务.
			<br>
			Gradle allows you to define one or more default tasks for your
				build.</p>
			<div class="example">
				<a name="defaultTasks"></a>
				<p class="title">
					<b>
					例 6.15. 定义默认任务<br>
					Example&nbsp;6.15.&nbsp;Defining a default tasks</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">defaultTasks <span
							xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'clean'</span>, <span
							xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'run'</span>

task clean &lt;&lt; {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'Default Cleaning!'</span>
}

task run &lt;&lt; {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'Default Running!'</span>
}

task other &lt;&lt; {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">"I'm not a default task!"</span>
}</pre>
					<p>
					 <strong class="userinput"><code>gradle
                                -q</code></strong>的输出结果.<br>
						Output of <strong class="userinput"><code>gradle
								-q</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q
Default Cleaning!
Default Running!</pre>
				</div>
			</div>
			<br class="example-break">
			<p>
			
			    这与直接调用<strong class="userinput"><code>gradle clean run</code></strong>效果是一样的.
			    在多项目构建中,每个子项目都可以指定单独的默认任务.如果子项目未进行指定将会调用父项目指定的的默认任务.
			  <br> 
				This is equivalent to running <strong class="userinput"><code>gradle
						clean run</code></strong>. In a multi-project build every subproject can have its
				own specific default tasks. If a subproject does not specify default
				tasks, the default tasks of the parent project are used (if
				defined).
			</p>
		</div>
		<div class="section">
			<div class="titlepage">
				<div>
					<div>
					    <h2 class="title">
                            <a name="configure-by-dag"></a>6.13.&nbsp; Configure by DAG
                        </h2>
					</div>
				</div>
			</div>
			<p>
			   稍后会对Gradle的配置阶段和运行阶段进行详细说明 (详见 <a class="xref"
                    href="../build_lifecycle.html">章节&nbsp;55, <i>构建的生命周期</i></a> )配置阶段后,Gradle会了解所有要执行的任务
                    Gradle提供了一个钩子来捕获这些信息.一个例子就是可以检查已经执行的任务中有没有被释放.借由此,你可以为一些变量赋予不同的值.
                    <br>
				As we later describe in full detail (see <a class="xref"
					href="../build_lifecycle.html">Chapter&nbsp;55, <i>The
						Build Lifecycle</i></a>), Gradle has a configuration phase and an
				execution phase. After the configuration phase, Gradle knows all
				tasks that should be executed. Gradle offers you a hook to make use
				of this information. A use-case for this would be to check if the
				release task is among the tasks to be executed. Depending on this,
				you can assign different values to some variables.
			</p>
			<p>
			    在下面的例子中,为<code class="literal">distribution</code>和<code class="literal">release</code>任务赋予了不同的
			  <code class="literal">version</code>值.<br>
				In the following example, execution of the
				<code class="literal">distribution</code>
				and
				<code class="literal">release</code>
				tasks results in different value of the
				<code class="literal">version</code>
				variable.
			</p>
			<div class="example">
				<a name="configByDagNoRelease"></a>
				<p class="title">
					<b>
					例 6.16. 依赖任务的不同输出<br>
					Example&nbsp;6.16.&nbsp;Different outcomes of build
						depending on chosen tasks</b>
				</p>
				<div class="example-contents">
					<p>
						<code class="filename">build.gradle</code>
					</p>
					<pre class="programlisting">task distribution &lt;&lt; {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">"We build the zip with version=$version"</span>
}

task release(dependsOn: <span xmlns:xslthl="http://xslthl.sf.net"
							class="hl-string">'distribution'</span>) &lt;&lt; {
    println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'We release now'</span>
}

gradle.taskGraph.whenReady {taskGraph -&gt;
    <span xmlns:xslthl="http://xslthl.sf.net" class="hl-keyword">if</span> (taskGraph.hasTask(release)) {
        version = <span xmlns:xslthl="http://xslthl.sf.net"
							class="hl-string">'1.0'</span>
    } <span xmlns:xslthl="http://xslthl.sf.net" class="hl-keyword">else</span> {
        version = <span xmlns:xslthl="http://xslthl.sf.net"
							class="hl-string">'1.0-SNAPSHOT'</span>
    }
}</pre>
					<p>
					 <strong class="userinput"><code>gradle
                                -q distribution</code></strong>的输出结果<br>
						Output of <strong class="userinput"><code>gradle
								-q distribution</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q distribution
We build the zip with version=1.0-SNAPSHOT</pre>
					<p>
					<strong class="userinput"><code>gradle
                                -q release</code></strong>的输出结果<br>
						Output of <strong class="userinput"><code>gradle
								-q release</code></strong>
					</p>
					<pre class="screen">&gt; gradle -q release
We build the zip with version=1.0
We release now</pre>
				</div>
			</div>
			<br class="example-break">
			<p>
			<code class="literal">whenReady</code>会在已发布的任务<span class="emphasis"><em>之前</em></span>影响到已发布任务的执行.
			    即使已发布的任务不是 <span class="emphasis"><em>主要</em></span>任务(也就是说,即使这个任务不是通过命令行直接调用)<br>
				The important thing is that
				<code class="literal">whenReady</code>
				affects the release task <span class="emphasis"><em>before</em></span>
				the release task is executed. This works even when the release task
				is not the <span class="emphasis"><em>primary</em></span> task
				(i.e., the task passed to the <span class="command"><strong>gradle</strong></span>
				command).
			</p>
		</div>
		<div class="section">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title">
							<a name="N103B3"></a>6.14.&nbsp;下一步目标
						</h2>
						<h4 class="title">
                            <a name="N103B3"></a>6.14.&nbsp;Where to next?
                        </h4>
					</div>
				</div>
			</div>
			<p>
                                           在本章中,我们了解了什么是task,但这还不够详细.欲知更多请参阅
                <a class="xref" href="../more_about_tasks.html">章节&nbsp;15, <i>任务进阶</i></a><br>
				In this chapter, we have had a first look at tasks. But this is not
				the end of the story for tasks. If you want to jump into more of the
				details, have a look at <a class="xref"
					href="../more_about_tasks.html">Chapter&nbsp;15, <i>More
						about Tasks</i>
				</a>.
			</p>
			<p>
			    另外,可以目录继续学习
			  <a class="xref" href="../tutorial_java_projects.html">第七章&nbsp;, <i>Java
                                        快速入门</i></a> 和 <a class="xref"
                    href="../artifact_dependencies_tutorial.html">第八章&nbsp;, <i>依赖管理基础</i></a>.<br>
				Otherwise, continue on to the tutorials in <a class="xref"
					href="../tutorial_java_projects.html">Chapter&nbsp;7, <i>Java
						Quickstart</i></a> and <a class="xref"
					href="../artifact_dependencies_tutorial.html">Chapter&nbsp;8, <i>Dependency
						Management Basics</i></a>.
			</p>
		</div>
		<div class="footnotes">
			<br>
			<hr align="left" width="100">
			<div class="footnote">
				<p>
					<sup>[<a href="#N101CA" name="ftn.N101CA" class="para">2</a>]
					附录中有命令可以更改这种默认行为. 请参阅 <a
                        class="xref" href="../gradle_command_line.html">附录&nbsp;D,
                        <i>Gradle 命令行</i>
                        <br>
					</sup>There are command line switches to change this behavior. See <a
						class="xref" href="../gradle_command_line.html">Appendix&nbsp;D,
						<i>Gradle Command Line</i>
					</a>)
				</p>
			</div>
		</div>
	</div>
	<div class="navfooter">
		<div>
			<div class="navbar">
				<a xmlns:xslthl="http://xslthl.sf.net"
					href="../ch05/troubleshooting.html"
					title="章节&nbsp;5.&nbsp;问题反馈">上一页</a><span>|</span><a
					xmlns:xslthl="http://xslthl.sf.net" href="../userguide.html"
					title="Gradle User Guide">返回目录</a><span>|</span><a
					xmlns:xslthl="http://xslthl.sf.net"
					href="../ch07/tutorial_java_projects.html"
					title="章节&nbsp;7.&nbsp;Java Quickstart">下一页</a>
			</div>
		</div>
	</div>
</body>
</html>